package com.dzl.render;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import com.dzl.util.EmptyUtils;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
 * 
 * @Description:操作excel
 * @author liufan
 * @time:2021年4月24日 下午3:58:34
 */
public class PoiKit {
	 public static final int HEADER_ROW = 1;
	 private static final String DEFAULT_SHEET_NAME = "new sheet";
	 private static final int MAX_ROWS = 65536;
	
	 /**
    * @param sheetName sheet名称
    * @param headerRow 设置头列占的行数
    * @param headers 头列值
    * @param columns 列key(即 list<Map<String ,Ojbect>> 中 map的key)
    * @param list 数据
    * @param cellWidth 设置单元格宽度
    * @return
    */
   public static HSSFWorkbook export(String sheetName, int headerRow, String[] headers, String[] columns,
           List<Map<String,Object>> list, int cellWidth) {
       boolean hasHeaders = false;
       int columnNum = Math.max(0, columns.length);
       if (headers.length > 0) {
           hasHeaders = true;
           columnNum = headers.length;
       }
       return export(sheetName, cellWidth, headerRow, headers, columns, list, columnNum, hasHeaders);
   }
   
   /**
    * 
    * @param sheetName sheet名称
    * @param cellWidth 设置单元格宽度
    * @param headerRow 设置头列占的行数
    * @param headers 头列值
    * @param columns 列key(即 list<Map<String ,Ojbect>> 中 map的key)
    * @param list 数据
    * @return
    */

   public static HSSFWorkbook export(String sheetName, int cellWidth, int headerRow, String[] headers,
           String[] columns, List<?> list, int columnNum, boolean hasHeaders) {
   	//目前只做单sheet
       if (EmptyUtils.isEmpty(sheetName)) {
           sheetName = DEFAULT_SHEET_NAME;
       }
       
       HSSFWorkbook wb = new HSSFWorkbook();
       HSSFSheet sheet = wb.createSheet(sheetName);
       HSSFRow row = null;
       HSSFCell cell = null;
       sheet.setDefaultColumnWidth(cellWidth);
       
       // 生成一个样式
       HSSFCellStyle style = wb.createCellStyle();
       // 设置这些样式
       style.setFillForegroundColor(HSSFColor.GREY_50_PERCENT.index);
       style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
       style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
       style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
       style.setBorderRight(HSSFCellStyle.BORDER_THIN);
       style.setBorderTop(HSSFCellStyle.BORDER_THIN);
       style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
       // 生成一个字体
       HSSFFont font = wb.createFont();
       font.setColor(HSSFColor.WHITE.index);
       font.setFontHeightInPoints((short) 12);
       font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
       // 把字体应用到当前的样式
       style.setFont(font);
       // 生成并设置另一个样式
       HSSFCellStyle style2 = wb.createCellStyle();
       style2.setFillForegroundColor(HSSFColor.WHITE.index);
       style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
       style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
       style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
       style2.setBorderRight(HSSFCellStyle.BORDER_THIN);
       style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
       style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
       style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
     
       // 生成另一个字体
      HSSFFont font2 = wb.createFont();
       font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
       // 把字体应用到当前的样式
       style2.setFont(font2);
       
       if (hasHeaders) {
           row = sheet.createRow(0);
           if (headerRow <= 0) {
               headerRow = HEADER_ROW;
           }
           headerRow = Math.min(headerRow, MAX_ROWS);
           for (int h = 0, lenH = headers.length; h < lenH; h++) {
               cell = row.createCell(h);
               cell.setCellStyle(style);
               HSSFRichTextString text = new HSSFRichTextString(headers[h]);
               cell.setCellValue(text);
           }
       }
       
       if (list.size() == 0) {
           return wb;
       }
       
       for (int i = 0, len = list.size(); i < len; i++) {
           row = sheet.createRow(i + headerRow);
           Object obj = list.get(i);
           if (obj == null) {
               continue;
           }
           if (obj instanceof Map) {
               processAsMap(columns, row, obj,style2);
           }
       }
       return wb;
   }
   
   @SuppressWarnings("unchecked")
   private static void processAsMap(String[] columns, HSSFRow row, Object obj, HSSFCellStyle style) {
       HSSFCell cell;
       
       Map<String, Object> map = (Map<String, Object>) obj;
       if (columns.length == 0) {// 未设置显示列，默认全部
           Set<String> keys = map.keySet();
           int columnIndex = 0;
           for (String key : keys) {
               cell = row.createCell(columnIndex);
               cell.setCellStyle(style);
               cell.setCellValue(EmptyUtils.isNotEmpty(map.get(key))?map.get(key)+"":"");
               columnIndex++;
           }
       } else {
           for (int j = 0, lenJ = columns.length; j < lenJ; j++) {
               cell = row.createCell(j);
               cell.setCellStyle(style);
               cell.setCellValue(EmptyUtils.isNotEmpty(map.get(columns[j]))?map.get(columns[j])+"":"");
           }
       }
   }
}
